package com.link.base.wechat.wxtoken.controller;

import com.link.base.wechat.wxtoken.model.WxToken;
import com.link.core.cllog.LCLogger;
import com.link.base.wechat.wxplatform.model.WxPlatform;
import com.link.base.wechat.wxtoken.model.WxJsTicket;
import com.link.base.wechat.wxtoken.service.WxTokenService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * access_token/js_ticket管理控制器
 *
 * @author ouyangzhenqiang
 * @date 2019/03/11 15:40
 */
@Controller
@RequestMapping("/link/wxToken")
public class WxTokenController {
    @Resource
    private WxTokenService wxTokenService;

    /**
     * 根据微信公众号应用Id获取js_ticket
     *
     * @author ouyangzhenqiang
     * @date 2019/03/11 15:41
     */
    @RequestMapping(value = {"/getJsTicket"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getJsTicket(WxPlatform record, HttpSession session,
                                                       HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            String wxAppId = record.getWx_app_id();
            if (StringUtils.isBlank(wxAppId)) {
                throw new ServiceException("WXTOKEN-001");
            }
            WxJsTicket wxJsTicket = wxTokenService.getWxJsTicketByAppId(wxAppId);
            result.put("success", true);
            result.put("js_ticket", wxJsTicket.getJs_ticket());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 获取微信AccessToken
     *
     * @author huanglongjian
     * @date 2019-11-05 15:50
     */
    @RequestMapping(value = {"/getWxAccessTokenByAppId"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getWxAccessTokenByAppId(@RequestParam("wxAppId") String wxAppId, HttpSession session,
                                                       HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (StringUtils.isBlank(wxAppId)) {
                throw new ServiceException("WXTOKEN-001");
            }
            WxToken wxToken = wxTokenService.getWxAccessTokenByAppId(wxAppId);
            result.put("success", true);
            result.put("result", wxToken);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

}
